package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.constans.StatusContants;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.DeviceAlertHandleDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.mapper.UserMapper;
import com.zzyl.nursing.service.IAlertDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 报警数据Service业务层处理
 *
 * @author alexis
 * @date 2025-06-14
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Resource
    private UserMapper UserMapper;
    @Autowired
    private TokenService tokenService;




    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return alertDataMapper.deleteById(id);
    }

    @Override
    public int handleAlertData(DeviceAlertHandleDto dahd, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        //1.先查询异常数据信息
        AlertData alertData = alertDataMapper.selectById(dahd.getId());
        if(alertData != null){
            alertData.setProcessingResult(dahd.getProcessingResult());
            alertData.setProcessingTime(dahd.getProcessingTime());
            alertData.setStatus(Integer.valueOf(StatusContants.HANDLED_STATUS));
            alertData.setProcessorId(loginUser.getUserId());
            alertData.setProcessorName(loginUser.getUsername());
        }
        int update = alertDataMapper.updateById(alertData);
        if (update <= 0){
            throw new BaseException("更新异常数据信息失败");
        }

        return update;
    }
}
